package com.java110.mall.goods.bmo.housekeepingType.impl;

import com.java110.dto.housekeeping.HousekeepingTypeDto;
import com.java110.mall.goods.bmo.housekeepingType.IGetHousekeepingTypeBMO;
import com.java110.intf.goods.IHousekeepingTypeInnerServiceSMO;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("getHousekeepingTypeBMOImpl")
public class GetHousekeepingTypeBMOImpl implements IGetHousekeepingTypeBMO {

    @Autowired
    private IHousekeepingTypeInnerServiceSMO housekeepingTypeInnerServiceSMOImpl;

    /**
     * @param housekeepingTypeDto
     * @return 订单服务能够接受的报文
     */
    public ResponseEntity<String> get(HousekeepingTypeDto housekeepingTypeDto) {


        int count = housekeepingTypeInnerServiceSMOImpl.queryHousekeepingTypesCount(housekeepingTypeDto);

        List<HousekeepingTypeDto> housekeepingTypeDtos = null;
        if (count > 0) {
            housekeepingTypeDtos = housekeepingTypeInnerServiceSMOImpl.queryHousekeepingTypes(housekeepingTypeDto);
            freshShopImg(housekeepingTypeDtos);
        } else {
            housekeepingTypeDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) housekeepingTypeDto.getRow()), count, housekeepingTypeDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        return responseEntity;
    }

    /**
     * @param housekeepingTypeDtos
     */
    private void freshShopImg(List<HousekeepingTypeDto> housekeepingTypeDtos) {
        String imgUrl = MappingCache.getValue(MappingConstant.FILE_DOMAIN,"IMG_PATH");

        imgUrl += (!StringUtil.isEmpty(imgUrl) && imgUrl.endsWith("/") ? "" : "/");

        for (HousekeepingTypeDto housekeepingTypeDto : housekeepingTypeDtos) {
            if (housekeepingTypeDto.getHktIcon().startsWith("http")) {
                continue;
            }
            housekeepingTypeDto.setHktIcon(imgUrl + housekeepingTypeDto.getHktIcon());
        }
    }


}
